/*
    XTU OJ: 1424
    Link: https://acm.xtu.edu.cn/exam/index.php/problem/exam_read/id/1424
    by: fifth_light
*/

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main(void) {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        char string[32768];
        char set[32];
        scanf(" %s %s", string, set);
        int ch[32] = {0};
        for (char* c = &set[0]; *c != '\0'; c++) {
            ch[*c - 'a'] = 1;
        }
        int length = 0;
        int max = 0;
        for (char* c = &string[0]; *c != '\0'; c++) {
            if (ch[*c - 'a']) {
                length++;
            } else {
                if (length > max) {
                    max = length;
                }
                length = 0;
            }
        }
        if (length > max) {
            max = length;
        }
        printf("%d\n", max);
    }
    return 0;
}
